﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using smartphone.App_Code;
using System.Text;

namespace smartphone.UserControls
{
    public partial class AdminPromotion : System.Web.UI.UserControl
    {
        Promotion promotion = new Promotion();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            GridViewPromotion.DataSource = promotion.GetAllPromotion();
            GridViewPromotion.DataBind();
        }

        /// <summary>
        /// Hien thi messagebox thong bao loi
        /// </summary>
        /// <param name="msg">Nội dung thông báo được đưa ra</param>
        private void ShowPopUpMsg(string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("alert('");
            sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
            sb.Append("');");
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
        }

        protected void GridViewPromotion_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridViewPromotion.EditIndex = e.NewEditIndex;

            BindGrid();
        }

        protected void GridViewPromotion_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string id = GridViewPromotion.DataKeys[e.RowIndex].Value.ToString();

            //lay ngay ket thuc
            string endDate = ((TextBox)GridViewPromotion.Rows[e.RowIndex].FindControl("TextBoxEnd")).Text;
            DateTime end = DateTime.ParseExact(endDate, "yyyy-MM-dd", null);

            //lay ngay bat dau
            string startDate = ((TextBox)GridViewPromotion.Rows[e.RowIndex].FindControl("TextBoxStart")).Text;
            DateTime start = DateTime.ParseExact(startDate, "yyyy-MM-dd", null);

            //lay noi dung khuyen mai moi
            string content = ((TextBox)GridViewPromotion.Rows[e.RowIndex].Cells[0].Controls[0]).Text;

       
            //khuyen mai co phai thuoc loai giam gia hay khong
            bool isDiscount;
            CheckBox check2 = (CheckBox)GridViewPromotion.Rows[e.RowIndex].Cells[3].Controls[0];
            isDiscount = check2.Checked;

            try
            {
                bool success = promotion.UpdatePromotion(id, end, start ,content, isDiscount);
                GridViewPromotion.EditIndex = -1;
            }
            catch(Exception ex)
            {
                ShowPopUpMsg(ex.ToString());
            }
            BindGrid();
        }

        protected void GridViewPromotion_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            bool success = false;
            int id = Int32.Parse(GridViewPromotion.DataKeys[e.RowIndex].Value.ToString());
            try
            {
                success = promotion.DeletePromotion(id);
            }
            catch
            {
                ShowPopUpMsg("Khuyến mại này đã gán cho sản phẩm.");
            }
            GridViewPromotion.EditIndex = -1;
            BindGrid();
        }

        protected void GridViewPromotion_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridViewPromotion.EditIndex = -1;
            BindGrid();
        }

    }
}